package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.Article;
import com.example.demo.entity.DTO.ArticleUserDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 文章表 Mapper 接口
 * </p>
 *
 * @author Mr.Yang
 * @since 2024-05-31
 */
@Mapper
public interface ArticleMapper extends BaseMapper<Article> {


    //根据userId查询对应用户写的文章
    @Select("SELECT t1.user_id,t1.user_name,t1.nickname,t1.head_img,t1.city,t2.article,t2.create_time \n" +
            "FROM user t1\n" +
            "INNER JOIN article t2\n" +
            "on t1.user_id=t2.user_id\n" +
            "WHERE t1.user_id = #{userId};\n")
    List<ArticleUserDTO> selectArticleAndUserById(@Param("userId") Integer userId);

    //模糊查询指定文章内容
    @Select("SELECT article_id,user_id,article,create_time,update_time,flag " +
            "FROM article WHERE article LIKE CONCAT('%', #{article}, '%')")
    List<Article> findByArticle(@Param("article") String article);

    /**
     * 根据内容模糊查询文章
     * @param article
     * @return
     */
    List<Article> selectByCriteria(@Param("article") Article article,@Param("userName") String userName);

    /**
     * 插入文章
     * @param article
     * @return
     */
    boolean insterArticle(Article article);

    @Select("SELECT * FROM article WHERE article_id = #{articleId}")
    Article selectArticleById(Integer articleId);
}
